package com.stock;

import com.kevin.core.util.DateUtils;
import com.kevin.core.util.StringUtils;
import com.kevin.dao.DayKDao;
import com.kevin.entity.DayK;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DayKService{
//	final static String api="http://xueqiu.com/stock/forchartk/stocklist.json?symbol={}&period=1day&type=normal&_=1448112126248";
	final static String api="http://xueqiu.com/stock/forchartk/stocklist.json?symbol={}&period=1000day&type=normal&begin={}&end={}&_=1448112126248";
	
	@Autowired
	DayKDao dao;
	
	public List<DayK> queryList(String stockCode,String beginDate,String endDate){
		beginDate = Long.valueOf(DateUtils.parseDate(beginDate).getTime()).toString();
		endDate = Long.valueOf(DateUtils.parseDate(endDate).getTime()).toString();
		String url=StringUtils.fromat(api, stockCode,beginDate,endDate);
		Logger.info("api:{}",url);
		String json=Client.doGet(url);
		Parser parser=new DayKParser();
		return (List<DayK>) parser.parse(json);
	}

	public void save(String stockCode,String beginDate,String endDate){
		List<DayK> list = this.queryList(stockCode, beginDate,endDate);
		Logger.info("{} {} to {} day K  size:{}",stockCode, beginDate,endDate,list.size());
		for(DayK k:list){
			k.setSymbol(stockCode);
			dao.save(k);
		}
	}


	public void deleteAll(){
		dao.deleteAll();
	}

}
